﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using GuRenFang.Service.Entity;
using GuRenFang.Service.IDa;

namespace GuRenFang.Da
{
    public class NewsRepository : RepositoryBase<News>, INewsRepository
    {
        public NewsRepository(IDatabaseFactory databaseFactory)
            : base(databaseFactory)
         {

         }

        public IEnumerable<News> GetNewsByPages(int pageIndex, int pageSize, string orderFiled,out int totalRecord)
        {
            var startRecord = pageSize * (pageIndex- 1);
            totalRecord = DataContext.News.Count();
            return this.DataContext.News.OrderByDescending(n=>n.CreatedTime).Skip(startRecord).Take(pageSize);
        }

       public Boolean SetReadTimes(int id,int readTimes)
       {
           var news = this.DataContext.News.Find(id);
           news.ReadTimes=readTimes;
           this.DataContext.News.Attach(news);
           this.DataContext.Entry(news).State = EntityState.Modified;
             
           return true;
           
       }

    }
}
